package com.example.controller;

import com.example.anno.Log;
import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author hyk~
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       //条件信息
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        //page:分页查询的页码，如果未指定，默认为1
        //pageSize:分页查询的每页记录数，如果未指定，默认为10
        /*  较为繁琐   通过 @RequestParam(defaultValue = " ") 来设置默认值
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
         */
        log.info("分页查询参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        //调用service分页查询
       PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作,ids:{}",ids);
        empService.delete(ids);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工操作,emp:{}",emp);
        empService.save(emp);
        return  Result.success();
    }

    //根据id查询数据
    //单条数据用实体类 多条数据用List<实体类类型>集合
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据id查询员工：{}",id);
        Emp emp = empService.select(id);
        return Result.success(emp);
    }

    @Log
    //修改数据  json格式数据用实体类接收 加注解@RequestBody
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        log.info("需要修改的员工信息：{}",emp);
        empService.updateEmp(emp);
        return Result.success();
    }
}
